package com.aglow.bluetoothspeaker.main.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.SeekBar;

import com.aglow.bluetoothspeaker.R;
import com.aglow.bluetoothspeaker.main.base.BaseActivity;
import com.aglow.bluetoothspeaker.main.ui.view.ColorPickView;
import com.aglow.bluetoothspeaker.model.event.CustomCommandEvent;
import com.aglow.bluetoothspeaker.utils.BluzManagerUtils;
import com.aglow.bluetoothspeaker.utils.CalculateUtils;
import com.aglow.bluetoothspeaker.utils.HexUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.SPUtils;
import com.facebook.stetho.common.LogUtil;
import com.orhanobut.logger.Logger;


import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

import butterknife.OnClick;

/**
 * Author: chufeng
 * Email: 392044178@qq.com
 * Date: 2018/9/12 11:09
 */
public class LedActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.colorPickerView)
    ColorPickView colorPickerView;
    @BindView(R.id.fl_color_picker)
    FrameLayout flColorPicker;
    @BindView(R.id.ib_return)
    ImageButton ibReturn;
    @BindView(R.id.checkBox_red)
    CheckBox checkBoxRed;
    @BindView(R.id.checkBox_yellow)
    CheckBox checkBoxYellow;
    @BindView(R.id.checkBox_orange)
    CheckBox checkBoxOrange;
    @BindView(R.id.checkBox_green)
    CheckBox checkBoxGreen;
    @BindView(R.id.checkBox_viridity)
    CheckBox checkBoxViridity;
    @BindView(R.id.checkBox_blue)
    CheckBox checkBoxBlue;
    @BindView(R.id.checkBox_purple)
    CheckBox checkBoxPurple;
    @BindView(R.id.checkBox_control)
    CheckBox checkBoxControl;
    //顔色checkbox
    CheckBox[] checkBoxList = new CheckBox[7];
    //模式checkbox
//    CheckBox[] checkBoxMode = new CheckBox[2];
    @BindView(R.id.cb_breath)
    CheckBox cbBreath;
    @BindView(R.id.cb_rhythm)
    CheckBox cbRhythm;
    @BindView(R.id.seekBar)
    SeekBar seekBar;

    private BluzManagerUtils mBluzManagerUtils;
    private static final int LIGHT_ID_MODE_RULE = 0x01;
    private static final int LIGHT_ID_MODE_BREATH = 0x02;
    private static final int LIGHT_ID_MODE_RHYTHM = 0x03;
    private static final int LIGHT_ID_STATE_OPEN = 01;
    private static final int LIGHT_ID_STATE_CLOSE = 00;
    private int selectRed;
    private int selectGreen;
    private int selectBlue;
    private boolean isReturnProgress;

    private int pointX = 0;
    private int pointY = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_led;
    }

    @Override
    public void onBackClick() {

    }

    @Override
    public void initData() {


        int displayHeight = CalculateUtils.getDisplayHeight();//获取屏幕高度(px)
        int displayWidth = (int) (CalculateUtils.getDisplayWidth() * 3.0f / 4);//屏幕宽度的3/4
        int colorPickerWidth = Math.min(displayWidth, displayHeight / 2);
        colorPickerView.setBigCircle(colorPickerWidth / 2 - ConvertUtils.dp2px(30));//减去边框宽度，大致为17dp
        //获取蓝牙工具
        mBluzManagerUtils = BluzManagerUtils.getInstance();
        mBluzManagerUtils.queryLed();//查询led参数

        Log.i("led", "oncreate");
//        checkBoxMode[0].setOnCheckedChangeListener((buttonView, isChecked) -> {
////            int mRed = SPUtils.getInstance().getInt("mRed", -1);
////            int mGreen = SPUtils.getInstance().getInt("mGreen", -1);
////            int mBlue = SPUtils.getInstance().getInt("mBlue", -1);
//            if (isChecked) {
//                if (checkBoxMode[1].isChecked()) {
//                    checkBoxMode[1].setChecked(false);
//                }
//                mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, selectRed, selectGreen, selectBlue);
//                Log.i("Led", "cbBreath true");
//
//            } else {
//                mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, selectRed, selectGreen, selectBlue);
//                Log.i("Led", "cbBreath flase");
//            }
//
//        });
        cbBreath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ((CheckBox) v).isChecked();
                if (isChecked) {
                    if (cbRhythm.isChecked()) {
                        cbRhythm.setChecked(false);
                    }
                    mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, selectRed, selectGreen, selectBlue);
                    Log.i("Led", "cbBreath true");

                } else {
                    mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, selectRed, selectGreen, selectBlue);
                    Log.i("Led", "cbBreath flase");
                }
            }
        });
        cbRhythm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ((CheckBox) v).isChecked();
                if (isChecked) {
                    if (cbBreath.isChecked()) {
                        cbBreath.setChecked(false);
                    }
                    mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RHYTHM, 0, 0, 0);
                } else {
                    mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, selectRed, selectGreen, selectBlue);
                }
            }
        });

        checkBoxList[0] = checkBoxRed;
        checkBoxList[1] = checkBoxYellow;
        checkBoxList[2] = checkBoxOrange;
        checkBoxList[3] = checkBoxGreen;
        checkBoxList[4] = checkBoxViridity;
        checkBoxList[5] = checkBoxBlue;
        checkBoxList[6] = checkBoxPurple;


        for (int i = 0; i < checkBoxList.length; i++) {
            checkBoxList[i].setOnCheckedChangeListener(this);
        }


    }


    @Override
    public void initView() {
        int mCircleX = SPUtils.getInstance().getInt("mCircleX", -1);
        int mCircleY = SPUtils.getInstance().getInt("mCircleY", -1);

        if (mCircleX != -1 && mCircleY != -1) {
            colorPickerView.setCircleXY(mCircleX, mCircleY);
        }

    }

    /**
     * led参数返回码
     *
     * @param event 事件
     */
    @SuppressWarnings("unused")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomCommand(CustomCommandEvent event) {
        if (event.getWhat() == BluzManagerUtils.KEY_ANS_LIGHT_CONTROL_STATE) {
            byte[] bytes = event.getBytes();
            String s = HexUtils.bytes2hex(bytes);
            String light_mode = s.substring(0, 1 + 1);
            String light_on_off = s.substring(2, 3 + 1);
            String white_bright_ness = s.substring(4, 6);
            switch (light_mode) { //(01 为正常模式 02 为呼吸模式 03 为音乐律动模式
                case "01":
                    break;
                case "02":
                    cbBreath.setChecked(true);
                    break;
                case "03":
                    cbRhythm.setChecked(true);
                    break;
            }
            switch (light_on_off) {//01 为开 00 为关
                case "01":
                    checkBoxControl.setChecked(true);
                    break;
                case "00":
                    checkBoxControl.setChecked(false);
                    break;

            }
            Integer seekParmes = Integer.valueOf(white_bright_ness, 16);
            isReturnProgress = true;
            seekBar.setProgress(seekParmes);
            isReturnProgress = false;
            Log.i("led", "s = " + HexUtils.bytes2hex(bytes));
            Log.i("led", "seekParmes = " + seekParmes);
            Log.i("led", "light_mode = " + light_mode);
            Log.i("led", "light_on_off = " + light_on_off);
            Log.i("led", "white_bright_ness = " + white_bright_ness);

        }


    }


    @Override
    public void addViewListener() {
        colorPickerView.setOnColorChangedListener(new ColorPickView.OnColorChangedListener() {
            @Override
            public void onColorChange(int red, int green, int blue) {
                //控制发送的数据量
                double sqrt = Math.sqrt
                        (Math.abs((pointX - colorPickerView.getCircleX()) * (pointX - colorPickerView.getCircleX()) +
                                (pointY - colorPickerView.getmCircleY()) * (pointY - colorPickerView.getmCircleY())));
                if (sqrt > 8) {
                    postColor(red,green,blue);
                }
                pointX = colorPickerView.getCircleX();
                pointY = colorPickerView.getmCircleY();

            }

            @Override
            public void onTouchStop(int red, int green, int blue) {
                postColor(red,green,blue);
                pointX = colorPickerView.getCircleX();
                pointY = colorPickerView.getmCircleY();
            }
        });


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (isReturnProgress) {
                    return;
                }
                mBluzManagerUtils.sendBrightness(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


//        checkBoxControl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    mBluzManagerUtils.setLightState(LIGHT_ID_STATE_OPEN);
//                } else {
//                    mBluzManagerUtils.setLightState(LIGHT_ID_STATE_CLOSE);
//                }
//            }
//        });

        checkBoxControl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = ((CheckBox) v).isChecked();
                if (isChecked) {
                    mBluzManagerUtils.setLightState(LIGHT_ID_STATE_OPEN);
                } else {
                    mBluzManagerUtils.setLightState(LIGHT_ID_STATE_CLOSE);
                }
            }
        });
    }


    @OnClick({R.id.ib_return, R.id.cb_breath, R.id.cb_rhythm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_return:
                finish();
//                showActivity(MainActivity.class);
                break;


        }
    }

    private void postColor(int red ,int green,int blue) {
        for (int i = 0; i < checkBoxList.length; i++) {
            if (checkBoxList[i].isChecked()) {
                checkBoxList[i].setChecked(false);
            }
        }
        selectRed = red;
        selectGreen = green;
        selectBlue = blue;
//        if (!cbRhythm.isChecked()) {//律动模式关闭

            if (cbBreath.isChecked()) {
                mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, red, green, blue);
                Log.i("Led", "BREATH"+"red = " + red + "green = " + green + "blue = " + blue);
            }else if(cbRhythm.isChecked()) {
                mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RHYTHM, red, green, blue);
                Log.i("Led", "RHYTHM"+"red = " + red + "green = " + green + "blue = " + blue);
            }else{
                 mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, red, green, blue);
                Log.i("Led", "RULE"+"red = " + red + "green = " + green + "blue = " + blue);
                }

//        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i("led ", "mBluzManagerUtils = " + mBluzManagerUtils);
        if (mBluzManagerUtils == null) {
            //获取蓝牙工具
            mBluzManagerUtils = BluzManagerUtils.getInstance();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.i("led", "onDestroy");
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (buttonView.isPressed()) {
            if (isChecked) {
                for (int i = 0; i < checkBoxList.length; i++) {
                    if (checkBoxList[i].getTag().equals(buttonView.getTag())) {
                        checkBoxList[i].setChecked(true);

                    } else {
                        checkBoxList[i].setChecked(false);
                    }
                }
                switch (buttonView.getId()) {
                    case R.id.checkBox_red:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 255, 0, 0);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 255, 0, 0);
                        }
                        break;
                    case R.id.checkBox_orange:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 240, 133, 26);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 240, 133, 26);
                        }
                        break;
                    case R.id.checkBox_yellow:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 235, 221, 5);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 235, 221, 5);
                            Log.i("led", "66666666666666666666666666");
                        }
                        break;
                    case R.id.checkBox_green:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 0, 156, 77);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 0, 156, 77);
                        }
                        break;
                    case R.id.checkBox_viridity:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 167, 201, 35);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 167, 201, 35);
                        }
                        break;
                    case R.id.checkBox_blue:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 0, 163, 232);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 0, 163, 232);
                        }
                        break;
                    case R.id.checkBox_purple:
                        if (cbRhythm.isChecked()) {
                            return;
                        }
                        if (cbBreath.isChecked()) {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_BREATH, 166, 33, 133);
                        } else {
                            mBluzManagerUtils.sendColor(LIGHT_ID_MODE_RULE, 166, 33, 133);
                        }
                        break;
                }


            }

        }
    }


}
